﻿Public Class frmRegistrarMovCaja 

    Private Sub frmRegistrarMovCaja_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tObservacion.Text = ""
        tMonto.EditValue = 0
        tAutoriza.Text = My.Forms.frmAutentificarMovCaja.usuario.Text
        tMonto.Focus()
    End Sub


    Private Sub btEntrada_Click(sender As System.Object, e As System.EventArgs) Handles btEntrada.Click
        If tMonto.EditValue <= 0 Then
            ErrorProvider1.SetError(tMonto, "Debe ingresar un monto.")
        ElseIf tObservacion.Text = "" Then
            ErrorProvider1.SetError(tObservacion, "Debe ingresar una observación.")
        Else
            ErrorProvider1.SetError(tMonto, Nothing)
            ErrorProvider1.SetError(tObservacion, Nothing)

            Try
                Dim idmov As Integer = 1
                If MovimientoscajaTableAdapter1.ScalarQueryUltimoId() Then
                    idmov = MovimientoscajaTableAdapter1.ScalarQueryUltimoId() + 1
                End If

                Dim ultiticket As Integer = 1
                If FacturasTableAdapter1.ScalarQueryObtieneUltimoNumFactura() Then
                    ultiticket = FacturasTableAdapter1.ScalarQueryObtieneUltimoNumFactura()
                End If

                Dim ultticket_ultcierre As Integer = 0
                If CierrecajaTableAdapter1.ScalarQueryUltimoCierreTicketFinal(My.Computer.Name) Then
                    ultticket_ultcierre = CierrecajaTableAdapter1.ScalarQueryUltimoCierreTicketFinal(My.Computer.Name)
                End If

                If ultiticket = ultticket_ultcierre Then
                    ultiticket = ultiticket + 1
                End If

                Dim idusuario As Integer = 1
                Try
                    idusuario = ConexionSGS.EjecutarQueryScalar(String.Format("select iduser from usuarios where nombreuser= '{0}'", tAutoriza.Text))
                Catch ex As Exception
                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                    AlertControl1.Show(Me, "Error", "Ha ocurrido un error obtener el Id de usuario.")
                    LogSGS.RegistrarLogYEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Error al obtener iduser con el nombre de usuario", ex, My.Resources.StringGlobal.mdlFacturacion, Name, sender.GetType.FullName.ToString())
                End Try

                MovimientoscajaTableAdapter1.InsertQuery(idmov, Today, TimeOfDay, My.Forms.MDI.Iduser.Caption, tMonto.EditValue, _
                                                        My.Computer.Name, ultiticket, True, tObservacion.Text, idusuario)

                AlertControl1.AppearanceCaption.ForeColor = Color.Green
                AlertControl1.Show(Me, "Notificación", "Se guardó el movimiento de caja.")
                My.Forms.frmAutentificarMovCaja.Close()
                Close()
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Me, "Error", "Ha ocurrido un error al generar el movimiento de caja.")
                LogSGS.RegistrarLogYEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Error al registrar un movimiento de caja (Entrada)", ex, My.Resources.StringGlobal.mdlFacturacion, Name, sender.GetType.FullName.ToString())
            End Try
        End If
    End Sub


    Private Sub btSalida_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSalida.Click
        If tMonto.EditValue <= 0 Then
            ErrorProvider1.SetError(tMonto, "Debe ingresar un monto.")
        ElseIf tObservacion.Text = "" Then
            ErrorProvider1.SetError(tObservacion, "Debe ingresar una observación.")
        Else
            ErrorProvider1.SetError(tMonto, Nothing)
            ErrorProvider1.SetError(tObservacion, Nothing)

            Try
                Dim idmov As Integer = 1
                If MovimientoscajaTableAdapter1.ScalarQueryUltimoId() Then
                    idmov = MovimientoscajaTableAdapter1.ScalarQueryUltimoId() + 1
                End If

                Dim ultiticket As Integer = 1
                If FacturasTableAdapter1.ScalarQueryObtieneUltimoNumFactura() Then
                    ultiticket = FacturasTableAdapter1.ScalarQueryObtieneUltimoNumFactura()
                End If

                Dim ultticket_ultcierre As Integer = 0
                If CierrecajaTableAdapter1.ScalarQueryUltimoCierreTicketFinal(My.Computer.Name) Then
                    ultticket_ultcierre = CierrecajaTableAdapter1.ScalarQueryUltimoCierreTicketFinal(My.Computer.Name)
                End If

                If ultiticket = ultticket_ultcierre Then
                    ultiticket = ultiticket + 1
                End If

                Dim idusuario As Integer = 1
                Try
                    idusuario = ConexionSGS.EjecutarQueryScalar(String.Format("select iduser from usuarios where nombreuser= '{0}'", tAutoriza.Text))
                Catch ex As Exception
                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                    AlertControl1.Show(Me, "Error", "Ha ocurrido un error obtener el Id de usuario.")
                    LogSGS.RegistrarLogYEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Error al obtener iduser con el nombre de usuario", ex, My.Resources.StringGlobal.mdlFacturacion, Name, sender.GetType.FullName.ToString())
                End Try

                MovimientoscajaTableAdapter1.InsertQuery(idmov, Today, TimeOfDay, My.Forms.MDI.Iduser.Caption, tMonto.EditValue, _
                                                        My.Computer.Name, ultiticket, False, tObservacion.Text, idusuario)

                AlertControl1.AppearanceCaption.ForeColor = Color.Green
                AlertControl1.Show(Me, "Notificación", "Se guardó el movimiento de caja.")
                My.Forms.frmAutentificarMovCaja.Close()
                Close()
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Me, "Error", "Ha ocurrido un error al generar el movimiento de caja.")
                LogSGS.RegistrarLogYEnviarCorreo(My.Resources.StringGlobal.prgAngely, "Error al registrar un movimiento de caja (Entrada)", ex, My.Resources.StringGlobal.mdlFacturacion, Name, sender.GetType.FullName.ToString())
            End Try
        End If
    End Sub

    Private Sub btCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btCancelar.Click
        My.Forms.frmAutentificarMovCaja.Close()
        Close()
    End Sub

    Private Sub frmRegistrarMovCaja_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        My.Forms.frmAutentificarMovCaja.Close()
    End Sub
End Class